<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Register</title>
<link rel="stylesheet" type="text/css" href="style/stylelogin.css">
<!--script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script-->
<script type="text/javascript" src="js/jquery-1.6.4.js"></script>
<script type="text/javascript" src="js/loginjs.js"></script> 

<script type="text/javascript" src="js/jquery-validation-1.9.0/jquery.validate.min.js"></script> 

<script type="text/javascript" src="js/validate.js"></script>
</head>

<body>
<?php 
if(isset($_POST['register'])) {
	include "cnn.php";
	$username = $_POST['username'];
	$password = $_POST['password'];
	$repassword = $_POST['repassword'];
	$fullname = $_POST['fullname'];
	$mail = $_POST['email'];
	$address = $_POST['address'];
	$phone = $_POST['phone'];
	// ngan sql injection : http://vi.wikipedia.org/wiki/SQL_injection
	if (get_magic_quotes_gpc()==false) {
                  $username = mysql_real_escape_string($username);
				  $password = mysql_real_escape_string($password);
				  $repassword = mysql_real_escape_string($repassword);
				  $mail = mysql_real_escape_string($mail);
				  $address = mysql_real_escape_string($address);
				  $phone = mysql_real_escape_string($phone);
				  $fullname = mysql_real_escape_string($fullname);
     }
	$user = array($username,$password,$repassword,$mail,$address,$phone,$fullname);
	$check = "select use_username from lib_user where use_username = '".$user[0]."'";
	$qcheck = mysql_query($check) or die(mysql_error()); 
	$rown = mysql_num_rows($qcheck);
    $passw = md5($user[1]);
	
	/*
	khi su dung ham md5() se sinh ra 1 chuoi gom 32 ki tu,
	 => phai de do dai password tren csdl hay input password voi maxlength > 32
	md5 la mot thuat toan ma hoa , la 1 dang su dung ham bam, 
	cac tinh chat cua ham bam:
	f(x) = y khi do,
	+ voi moi y' # y thi : f(x) # y'
	+ voi moi x' # x thi : f(x') # y
	=> mot gia tri x tuong ung duy nhat voi chi mot gia tri y va nguoc lai
	=> muc dich su dung: dam bao tinh bao mat mat khau cho nguoi dung
	*/   
	
	  
		 if($rown != 0) {
			 
			 echo "<script> 
			 alert('username da ton tai, moi ban dang ky lai nhe');
			 </script>";
			
		
		 }
		 else {
			  $sql = "INSERT INTO lib_user(use_username,use_password,use_email,use_address,use_phone,use_level,use_fullname)values('".$user[0]."','".$passw."','".$user[3]."','".$user[4]."','".$user[5]."',2,'".$user[6]."')";
	              $query = mysql_query($sql)or die(mysql_error());
		echo "
		<script> 
		var p = 'ban da dang ki thanh cong, ban co muon dang nhap?';
		if(confirm(p)) {
			
			window.location.href='login.php';
		}
		else {
			
			window.location.href='home.php';
		}
		</script>
		
		";
			 }
	
	
}
?>
<a href="#register-box" class="register-window"><img src="img/register.png"></a> 
<div id="register-box" class="login-popup">
        <a href="#" class="close"><img src="img/close.png" class="btn_close" title="Close Window" alt="Close" /></a>
          <form method="post" class="signin" action="#" id = "input_form" name="formre">
                <fieldset class="textbox">
                <label class="username">
                <span>Username </span>
                <input id="username" name="username" value="" type="text" class="required" minlength="2" maxlength="20">
                <!-- class="required" minlength="2" maxlength="20" la dieu kien bat buoc phai co de validate() kiem tra                du lieu dau vao-->
                </label>
                <label class="password">
                <span>Password</span>
                <input id="password" name="password" value="" type="password" class="required" minlength="6" >
                </label>
                <label class="re-password">
                <span>Re-password</span>
                <input id="repassword" name="repassword" value="" type="password" class="required" equalTo="#password" >                <!--equalTo="#password" => re-password == password -->
                </label>
                 <label class="fullname">
                <span>FullName </span>
                <input id="fullname" name="fullname" value="" type="text" class="required" minlength="5" maxlength="50"  >
                </label>
                <label class="address">
                <span>Address </span>
                <input id="address" name="address" value="" type="text" class="required" minlength="5" maxlength="50"  >
                </label>
               
                <label class="email">
                <span>Email </span>
                <input id="email" name="email" value="" type="email" class="required email" maxlength="50">
                <!--class="required email" => de ham validate() kiem tra co phai la mot dia chi mail hop le ko?-->
                </label>
                <label class="phonenumber">
                <span>Phonenumber </span>
                <input id="phone" name="phone" value="" type="text"  minlength="10" class="required" maxlength="13">
                </label>
                
                 <input class="subm" type="submit" value="Register" name="register">
                    
                </fieldset>
          </form>
</div>   
</body>
</html>